from django.http import HttpResponse
from django.shortcuts import render
from .models import PersonModel


# Create your views here.

def test01(request):
    return HttpResponse('model test01')


# 数据库的基本操作
def test02(request):
    # test03()
    test04()
    return HttpResponse('model test02')


#  创建用户
def test03():
    create_model = PersonModel.objects.create(name='zs', last_name="张", age=19, adder="四川")
    create_model.save()
    p = PersonModel(name='ls', last_name="李", age=29, adder="北京")
    p.save()
    return None


# 查询用户
def test04():
    # 查询ID是1的用户
    q = PersonModel.objects.get(id=1)
    print(q)
    print('查询所有用户的信息')
    x = PersonModel.objects.all()
    for i in x:
        print(i)
    # 查询年龄大于20的
    x1 = PersonModel.objects.get(age__gt=20)
    print(x1)
    # 排序 降徐排序
    x2 = PersonModel.objects.order_by('-age')
    print(x2)
    # 返回第一条和最后一条数据
    x3 = PersonModel.objects.first()
    print(x3)
    x4 = PersonModel.objects.last()
    print(x4)
    # orm框架查询某条数据的单条字段 名字和id   这个返回的结果是列表包字典
    x5 = PersonModel.objects.values('id', 'name')
    for i in x5:
        print(i['id'])
        print(i['name'])
    # 使用列表包元组的形式
    x6 = PersonModel.objects.values_list('id', 'name')
    for i in x6:
        print(i)
    return None
